import { weekList } from '@/api/ds';

export function formatOperatingPeriod(source) {
  source ||= [];

  if (typeof source === 'string') {
    source = JSON.parse(source || '[]');
  }

  for (const item of source) {
    const { week: label } = item;
    item.value = weekList.find((o) => o.label === label)?.value ?? 0;
  }

  source.sort((a, b) => a.value - b.value);

  const len = source.length;
  const result = [];

  const inxSet = new Set();

  for (let i = 0; i < len; i++) {
    const { value: v1, week: w1, times: st } = source[i];
    if (!st || inxSet.has(i)) {
      continue;
    }

    inxSet.add(i);
    const incWeek = [{ value: v1, label: w1 }];

    for (let j = i + 1; j < len; j++) {
      const { value: v2, week: w2, times: et } = source[j];
      if (!et || inxSet.has(j)) {
        continue;
      }

      if (st.start === et.start && st.end === et.end) {
        inxSet.add(j);
        incWeek.push({ value: v2, label: w2 });
      }
    }

    const startTime = st.start,
      endTime = st.end;
    result.push({ startTime, endTime, incWeek });
  }

  restOperatingPeriod(result);

  return result;
}

export function restOperatingPeriod(source) {
  source ||= [];

  for (const item of source) {
    const { startTime, endTime, incWeek } = item;
    item.timeDesc = startTime + '&nbsp;-&nbsp;' + endTime;

    incWeek.sort((a, b) => a.value - b.value);

    const len = incWeek.length;
    const pureWeek = [];

    for (let i = 0; i < len; i++) {
      const { value: v1, label } = incWeek[i];
      let q = i,
        v0 = v1;

      for (let j = i + 1; j < len; j++) {
        const { value: v2 } = incWeek[j];
        if (v2 - v0 === 1) {
          q = j;
          v0 = v2;
        } else {
          break;
        }
      }

      if (i === q) {
        pureWeek.push(label);
      } else {
        const label2 = incWeek[q].label;

        if (q - i > 1) {
          pureWeek.push(`${label}至${label2}`);
        } else {
          pureWeek.push(label);
          pureWeek.push(label2);
        }
        i = q;
      }
    }

    item.pureWeek = pureWeek;
    item.weekDesc = pureWeek.join('、');
  }
}

export function saveOperatingPeriod(source, startTime, endTime, incWeek) {
  source ||= [];

  let item = source.find((o) => o.startTime === startTime && o.endTime === endTime);
  if (!item) {
    source.push({ startTime, endTime, incWeek });
    restOperatingPeriod(source);
    return;
  }

  item.incWeek = incWeek;
  restOperatingPeriod(source);
}

export function deleteOperatingPeriod(source, startTime, endTime) {
  source ||= [];

  const index = source.findIndex((o) => o.startTime === startTime && o.endTime === endTime);
  if (index > -1) {
    source.splice(index, 1);
  }

  return [...source];
}

export function getExistWeek(source, startTime, endTime) {
  source ||= [];

  return source.reduce((prev, next) => {
    if (next.startTime !== startTime && next.endTime !== endTime) {
      next.incWeek.forEach((o) => prev.add(o.value));
    }

    return prev;
  }, new Set());
}

export function getOperatingPeriodWeek(source) {
  source ||= [];

  const result = [];
  for (const item of source) {
    const { startTime: start, endTime: end, incWeek } = item;

    for (const o of incWeek) {
      result.push({ week: o.label, times: { start, end } });
    }
  }

  return result;
}
